package com.longma.server.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.longma.server.constant.enums.UserTypeEnum;
import com.longma.server.dao.SysUserDAO;
import com.longma.server.dao.YuncangDAO;
import com.longma.server.dao.YuncangUserDAO;
import com.longma.server.entity.SysUser;
import com.longma.server.entity.Yuncang;
import com.longma.server.entity.YuncangUser;
import com.longma.server.service.IYuncangUserService;
import com.longma.server.util.AuthContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HAIBO
 * @since 2021-04-28
 */
@Service
public class YuncangUserServiceImpl implements IYuncangUserService {
    @Autowired
    private YuncangDAO yuncangDAO;

    @Autowired
    private SysUserDAO sysUserDAO;

    @Autowired
    private YuncangUserDAO yuncangUserDAO;

    /**
     * 获取当前用户有权限的云仓
     */
    public List<Yuncang> getYuncangs() {
        return getYuncangs(AuthContextUtil.getLoginName());
    }


    /**
     *  获取指定用户关联的云仓
     */
    @Override
    public List<Yuncang> getYuncangs(String loginName) {
        List<Yuncang> result = new ArrayList<>();
        if(StrUtil.isBlank(loginName)){
            return result;
        }

        SysUser user = sysUserDAO.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getLoginName, loginName));
        if(user == null){
            return result;
        }

        if (UserTypeEnum.ADMINISTRATOR.equals(user.getType())){
            return yuncangDAO.list();
        }else{
            List<YuncangUser> items = yuncangUserDAO.list(Wrappers.<YuncangUser>lambdaQuery() .eq(YuncangUser::getUserId, user.getId()));
            List<Integer> yuncangIds = items.stream().map(YuncangUser::getYuncangId).collect(Collectors.toList());
            if(CollUtil.isEmpty(yuncangIds)) {
                return result;
            }
            return yuncangDAO.listByIds(yuncangIds);
        }
    }

    /**
     *  获取指定用户关联的云仓id列表
     */
    @Override
    public List<Integer> getYuncangIds(String loginName) {
        List<Integer> result = new ArrayList<>();
        if(StrUtil.isBlank(loginName)){
            return result;
        }

        SysUser user = sysUserDAO.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getLoginName, loginName));
        if(user == null){
            return result;
        }

        // 管理员 所有权限
        if (UserTypeEnum.ADMINISTRATOR.equals(user.getType())){
            return yuncangDAO.list(Wrappers.<Yuncang>lambdaQuery().select(Yuncang::getId)).stream().map(Yuncang::getId).collect(Collectors.toList());
        }else{
            List<YuncangUser> items = yuncangUserDAO.list(Wrappers.<YuncangUser>lambdaQuery().select(YuncangUser::getYuncangId)
                    .eq(YuncangUser::getUserId, user.getId()));
            return items.stream().map(YuncangUser::getYuncangId).collect(Collectors.toList());
        }
    }

    /**
     * 根据云仓id获取有权限的用户
     */
    @Override
    public Set<SysUser> getUsers(Integer yuncangId) {
        Set<SysUser> result = new HashSet<>();
        if(yuncangId == null){
            return result;
        }

        // 获取有权限的用户
        List<Integer> userIds = yuncangUserDAO.list(Wrappers.<YuncangUser>lambdaQuery().eq(YuncangUser::getYuncangId,yuncangId))
                .stream().map(YuncangUser::getUserId).collect(Collectors.toList());


        // 获取所有管理员
        List<SysUser> users;
        if(CollUtil.isNotEmpty(userIds)) {
            users = sysUserDAO.list(Wrappers.<SysUser>lambdaQuery().and(i -> i.eq(SysUser::getType, UserTypeEnum.ADMINISTRATOR)
                    .or().eq(SysUser::getId, userIds)));
        }else{
            users = sysUserDAO.list(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getType, UserTypeEnum.ADMINISTRATOR));
        }

        if(CollUtil.isNotEmpty(users)) {
            result.addAll(users);
        }
        return result;
    }

    @Override
    public void addYuncangUser(Yuncang yuncang) {
        // 将云仓关联用户信息保存至yuncang_user
        YuncangUser yuncangUser = new YuncangUser();
        SysUser sysUser = sysUserDAO.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getRealName,yuncang.getYuncanguserName()));
        if(sysUser!=null){
            yuncangUser.setUserId(sysUser.getId());
            yuncangUser.setYuncangId(yuncangDAO.getOne(Wrappers.<Yuncang>lambdaQuery().eq(Yuncang::getYuncangName,yuncang.getYuncangName())).getId());
            yuncangUserDAO.save(yuncangUser);
        }
    }

    @Override
    public void updateYuncangUser(Yuncang yuncang) {
        // 在云仓用户表查询该云仓是否有对应的管理员
        YuncangUser yuncangUser = yuncangUserDAO.getOne(Wrappers.<YuncangUser>lambdaQuery().eq(YuncangUser::getYuncangId,yuncang.getId()));
        // 若有
        if(yuncangUser!=null){
            // 获取系统用户表中与设置管理员对应的用户名
            SysUser sysUser = sysUserDAO.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getRealName,yuncang.getYuncanguserName()));
            // 若设置的云仓管理员发生变化
            if(yuncang.getYuncanguserName()!=null){
                if(!sysUser.getRealName().equals(yuncang.getYuncanguserName())){
                    yuncangUser.setUserId(sysUser.getId());
                    yuncangUserDAO.updateById(yuncangUser);
                }
            }
        }else{
            this.addYuncangUser(yuncang);
        }

    }
}
